//////////////////////////////////////////////////////////////////////////////////////////////
//Class Name: ValidateUserToRedirect
//Autor: Víctor Galván Chávez
//Objective: This class Builds the menu and submenu for user depending of the type of user. In this
//case System-Administrator (Human Resources Person) or Custom - Portal User (Employee)
//////////////////////////////////////////////////////////////////////////////////////////////
public class ValidateUserToRedirect {

    public ValidateUserToRedirect(accountQueryController controller) {
        
    }


    public ValidateUserToRedirect(customControllerCalendarArea controller) {

    }


    public ValidateUserToRedirect(Select_Test controller) {

    }


    public ValidateUserToRedirect(IERerenderBug controller) {

    }

        //Attributes
    public Contact contact{get;set;}
    public Account account{get; set;}
    public PageReference pageToGo{get;set;}
    public String labelForButton{get;set;}
    public String styleForCreateAnnouncementButton{
        get{
                        String profileId = Userinfo.getProfileId();
                Profile profile = [SELECT Name FROM Profile WHERE Id=:profileId];
                String mainMenu;
                if(profile.Name=='Custom - Portal Employee'){
                        styleForCreateAnnouncementButton = 'display:none;';
                }
                if(profile.Name=='System Administrator'){
                        mainMenu = 'homeHRMS';
                }
                return styleForCreateAnnouncementButton;
        }
        set;
    }
    //Objective: This attribute contains the main page depending of type of user.
    public String mainPage{
        get{
                String profileId = Userinfo.getProfileId();
                Profile profile = [SELECT Name FROM Profile WHERE Id=:profileId];
                String mainMenu;
                if(profile.Name=='Custom - Portal Employee'){
                        mainMenu = 'homeHRMSEmployee';
                }
                if(profile.Name=='System Administrator'){
                        mainMenu = 'homeHRMS';
                }
                return mainMenu;
                }
                set;
    }
        
//*Objective: This attribute contains the submenu for the user depending of it's type
        public List<MenuCreator> userSubmenu{
                get{
                        //System.debug('################################### Inside Get User Menu');
                        String profileId = Userinfo.getProfileId();
                Profile profile = [SELECT Name FROM Profile WHERE Id=:profileId];
                userSubmenu=new List<MenuCreator>();
                String url = ApexPages.currentPage().getUrl();
                        String pageName = url.split('apex/')[1];
                        
                        if(url.contains('?')){
                                pageName=pageName.split('\\?')[0];
                        }
                        
                        pageName = pageName.toUpperCase();
                        
                        //Verify if the current user is an employee and build it's menu
                if(profile.Name=='Custom - Portal Employee'){
                        if(pageName=='MYINFORMATION'){
                                //User doesn't have sub menu in this page
                                
                        }
                        if(pageName=='TRAINING'){
                                userSubmenu.add(new MenuCreator('MyCoursesPortal','My Courses'));
                        }
                        if(pageName=='TASKS'){
                                userSubmenu.add(new MenuCreator('#','#'));
                        }
                        if(pageName=='ANNOUNCEMENTS'){
                                
                        }
                        if(pageName=='HOLIDAYS'){
                                userSubmenu.add(new MenuCreator('#','Request Vacations'));
                        }
                        System.debug('---------------User Submenu: '+userSubmenu);
                }
                
                //If the user is administrator, build the complete submenú
                if(profile.Name=='System Administrator'){
                        //System.debug('------------------------Entro a construir el submenú de administrador');
                        //System.debug('Nombre de la página: '+pageName);
                        if(pageName=='EMPLOYEES' || pageName=='BAJAEMPIE'){
                                //System.debug('----------------------------La página es de Employees');
                                userSubmenu.add(new MenuCreator('EmployeeWizardStep1','New Employee'));
                                userSubmenu.add(new MenuCreator('BajaEmpIE','Fire Employee'));
                        }
                        if(pageName=='TRAINING' || pageName=='MyCoursesPortal' || pageName=='NewCourseSite' || pageName=='AddParticipantToCoursePortal'){
                                //userSubmenu.add(new MenuCreator('MyCoursesPortal','My Courses'));
                                userSubmenu.add(new MenuCreator('NewCourseSite','New Course'));
                                userSubmenu.add(new MenuCreator('AddParticipantToCoursePortal','Participant'));
                        }
                        if(pageName=='TASKS'){
                                
                        }
                        if(pageName=='ANNOUNCEMENTS'){
                                
                        }
                        if(pageName=='HOLIDAYS'){
                                userSubmenu.add(new MenuCreator('#','Request Vacations'));
                        }
                        
                        //System.debug('---------------User Menu: '+userMenu);
                }
                
                return userSubmenu;
                }
                set;
        }

//*Objective: This attribute contains the menu for the user depending of it's type
        public List<MenuCreator> userMenu{
                get{
                        String profileId = Userinfo.getProfileId();
                Profile profile = [SELECT Name FROM Profile WHERE Id=:profileId];
                        userMenu = new List<MenuCreator>();
                        System.debug('-------------Nombre del perfil: '+profile.Name);
                        
                        //Menu option Employees/MyInformation
                        if(profile.Name=='System Administrator'){
                                //userMenu.add(new MenuCreator('Employees','Employees','btnEmployeesSection','check();'));
                                userMenu.add(new MenuCreator('Employees','Employees','btnEmployeesSection'));
                        }
                        
                        if(profile.Name=='Custom - Portal Employee'){
                                //userMenu.add(new MenuCreator('MyInformation','My Information','btnEmployeesSection', 'check();'));
                                userMenu.add(new MenuCreator('MyInformation','My Information','btnEmployeesSection'));
                        }
                        /////////////////////////////////////////////////////////////
                        
                        //Menu option Training
                        if(profile.Name=='System Administrator'){
                                userMenu.add(new MenuCreator('Training','Training','btnTrainingSection'));
                        }
                        if(profile.Name=='Custom - Portal Employee'){
                                userMenu.add(new MenuCreator('MyCoursesPortal','My Training','btnTrainingSection'));
                        }
                        /////////////////////////////////////////////////////////////
                        
                        //RH=AnnouncementTasks
                        //Employee=Ann_Task_My_SelectorPage
                       
                       //Menu option Tasks/My Tasks
                        if(profile.Name=='Custom - Portal Employee'){
                                userMenu.add(new MenuCreator('Ann_Task_My_SelectorPage','My Tasks','btnTasksSection'));
                        }
                       
                        if(profile.Name=='System Administrator'){
                                userMenu.add(new MenuCreator('AnnouncementTasks','Tasks','btnTasksSection'));
                        }
                        ///////////////////////////////////////////////////////////////
                        
                        //userMenu.add(new MenuCreator('AnnouncementTasks','Tasks','btnTasksSection'));
                        
                        //Menu option Announcements
                        if(profile.Name=='System Administrator'){
                                userMenu.add(new MenuCreator('Announcements','Announcements','btnAnnouncementsSection'));
                        }
                        if(profile.Name=='Custom - Portal Employee'){
                                userMenu.add(new MenuCreator('Test_select','My Tests','btnAnnouncementsSection'));
                        }
                        
                        ////////////////////////////////////////////////////////////////
                        
                        //Menu option Holidays
                        //userMenu.add(new MenuCreator('Holidays','Holidays','btnHolidaysSection'));
                        
                        if(profile.Name=='System Administrator'){
                                userMenu.add(new MenuCreator('Event','Holidays','btnHolidaysSection'));
                        }
                        if(profile.Name=='Custom - Portal Employee'){
                            userMenu.add(new MenuCreator('RequestPersonalTimeOff','Holidays','btnHolidaysSection'));    
                        }
                        ////////////////////////////////////////////////////////////////
                        
                                                
                        //System.debug('--------------User menu: '+userMenu);
                        return userMenu;        
                }
                set;
                
        }
        
        
    public ValidateUserToRedirect(AccountStandardController controller) {
                
    }
    
    public ValidateUserToRedirect(ApexPages.StandardController controller) {
                
    }


    public ValidateUserToRedirect(Ann_Tsk_Std_Cntlr controller) {
                
    }

    public ValidateUserToRedirect() {
      
    }
    
    public ValidateUserToRedirect(Announcement_Controller controller) {
                
    }
   
    
//Objective: This method saves the basic (Datebirth, Marital Status) personal information of an 
//employee.
    public PageReference save(){
        try{
            //System.debug('Datos de employeeData: '+employeeData);
            //Id userId = UserInfo.getUserId();
            //List<User> contactId = [SELECT ContactId FROM User WHERE Id=:userId LIMIT 1];
            //contact = [SELECT Id, Salutation, FirstName, LastName, Account.isPersonAccount, 
                          //Account.Marital_Status__c, Account.Birthdate__c FROM Contact WHERE 
                          //Id=:contactId.get(0).ContactId];
            
            //contact.Account.Salutation = contact.Salutation;
            //contact.Account.Salutation = employeeData.Salutation;
            employeeData.Account.Salutation = employeeData.Salutation;
            //update contact.Account;
            //this.employeeData = contact;
            update employeeData.Account;
            
            return null;
        }catch(Exception ex){
            System.debug('An error has ocurred (ValidateUserToRedirect): save '+ex.getMessage());
            return null;
        }
    }
    
//Objective: This attribute contains the basic personal information of an employee. 
        public Contact employeeData{
                get{
                                try{
                                if(employeeData==null){
                    //System.debug('-------------------Validate user to redirect method--------------------');
                    //String profileId = Userinfo.getProfileId();
                    //Profile profile = [SELECT Name FROM Profile WHERE Id=:profileId];
                    //System.debug('Id del perfil de usuario: '+profile.Name);
                        Id userId = UserInfo.getUserId();
                        //System.debug('Id del usuario: '+userId);
                        List<User> contactId = [SELECT ContactId FROM User WHERE Id=:userId LIMIT 1];
                        
                        //System.debug('Lista de usuarios: '+contactId);
                        //System.debug('Id del contacto para buscar información: '+contactId.get(0).contactId);
                        employeeData = [SELECT Id, Salutation, FirstName, LastName, Account.isPersonAccount, 
                                   Account.Marital_Status__c, Account.Birthdate__c FROM Contact WHERE Id=:contactId.get(0).ContactId];
                        //System.debug('Contact llenado: '+contact);
                        //pageToGo = Page.MyInformation
                         //contact = employeeData;
                         
                                }
                                return employeeData;
                    
                }catch(Exception ex){
                    ex.getMessage();
                    return null;
                }
                }
                set;
        }
        
//*Objective: This method sends the user to it's main menu depending of it's type
        public PageReference goToMainMenu(){
                String profileId = Userinfo.getProfileId();
        Profile profile = [SELECT Name FROM Profile WHERE Id=:profileId];
        PageReference mainMenu;
        if(profile.Name=='Custom - Portal Employee'){
                mainMenu = Page.homeHRMSEmployee;
        }
        if(profile.Name=='System Administrator'){
                mainMenu = Page.homeHRMS;
        }
        return mainMenu;
        }
    
}